package com.flyee.controller.tools.cartoon.model
{
	import com.flyee.controller.tools.cartoon.control.AppearanceLoadManager;
	import com.flyee.controller.tools.cartoon.control.AppearanceMananger;
	import com.flyee.utils.GCHleper;
	import com.flyee.utils.MyLog;
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.textload.TextLoader;
	
	import fl.motion.Animator;
	import fl.motion.Keyframe;
	import fl.motion.Motion;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.ColorTransform;
	
	public class EquipObj extends EventDispatcher
	{
		[Bindable]
		public var part:Number = -1;
		[Bindable]
		public var initX:Number = 0;
		[Bindable]
		public var initY:Number = 0;
		[Bindable]
		public var motionXML:String = null;
		//动作类型
		public var type:uint = 0;
		//动作方向
		public var dir:uint = 0;
		//动作深度
		public var index:Number = 0;
		//加载共享库路径
		private var loaderURL:String = null;
		//动画对象
		public var myAnimator:Animator = null;
		//动画显示对象
		public var myDisplayObj:DisplayObject = null;
		//UUID
		public var myID:String = null;
		//初始颜色
		public var color:ColorTransform = null;
		//替换装备
		public var mySwfURL:String = null;
		
		public function EquipObj()
		{
		}
		public function setData(model:CreateEquipObjModel):void
		{
			this.type = model.type;
			this.dir = model.dir;
			this.part = model.part;
			this.loaderURL = model.loadURL;
			this.myID = model.myID;
			this.mySwfURL = model.mySwfURL;
			this.color = model.color;
			if(this.mySwfURL && this.mySwfURL.length>10)
			{
				loadEquip();
			}
			else
			{
				loadConfig();
			}
		}
		public function loadEquip():void
		{
			var tempLoader:LoaderModel = AppearanceLoadManager.getInstance().getLoader(this.mySwfURL);
			if(tempLoader)
			{
				this.getEquip(tempLoader)
			}
			else
			{
				AppearanceLoadManager.getInstance().regist(this.mySwfURL,this.getEquip,this.myID);
			}
		}
		public function getEquip(loader:LoaderModel):void
		{
			//this.myDisplayObj = loader;
			loadConfig();
		}
		public function setMySwfURL(t:LoaderModel):void
		{
			//this.mySwfURL = t.loadURL;
			var tempMotion:Motion = Motion.fromXMLString(this.motionXML);
			
			var tempClass:Class = null;
				
			var tempXML:XML = new XML(this.motionXML)
				
			var empty1:Namespace = tempXML.namespace("");
			var tempXMLL:XMLList = tempXML.empty1::source;
				
			var empty2:Namespace = tempXMLL.namespace("");
			var tempXMLL2:XMLList = tempXMLL.empty2::Source;
				
			var className:String = tempXMLL2.attribute("class");
			
			tempClass = AppearanceLoadManager.getInstance().getClass(this.mySwfURL,className);
			
			var tempDisplayObj:DisplayObject = new tempClass();
				
			var tempKeyFrame:Keyframe = tempMotion.keyframes[0];
			
			this.setDisplayObject(tempDisplayObj,tempKeyFrame.color);
		}
		private function setDisplayObject(t:DisplayObject,initColor:ColorTransform):void
		{
			var tempDisplayC:DisplayObjectContainer = null;
			if(this.myDisplayObj)
			{
				if(this.myDisplayObj.parent)
				{
					tempDisplayC = this.myDisplayObj.parent;
					this.myDisplayObj.parent.removeChild(this.myDisplayObj);
				}
			}
			var tempModel:CreateAppearanceModel = AppearanceMananger.getInstance().getAppCartoon(this.myID);
			
			if(tempModel)
			{
				var tempEquipXMLHelper:EquipXMLHelper = tempModel.infoManager.getInfo();
				var tempEquips:EquipsObj = tempEquipXMLHelper.getEquips(this.type,this.dir);
				tempEquips.removeEquip(this.part)
				this.myDisplayObj = t;
				if(tempEquips)
				{
					tempEquips.addEquip(this.part,this);
				}
			}
			else
			{
				MyLog.debug("error by ChangeEquip type: "+this.type +" dir: "+this.dir +" part: "+this.part);
			}
			
			
			myDisplayObj.x = this.initX;
			myDisplayObj.y = this.initY;
			
			myDisplayObj.transform.colorTransform = initColor;
			if(this.color)
			{
				myDisplayObj.transform.colorTransform.alphaOffset = this.color.alphaOffset;
				myDisplayObj.transform.colorTransform.redOffset = this.color.redOffset;
				myDisplayObj.transform.colorTransform.greenOffset = this.color.greenOffset;
				myDisplayObj.transform.colorTransform.blueOffset = this.color.blueOffset;
			}
			
			if(this.myAnimator)
			{
				this.myAnimator.target = this.myDisplayObj;
			}
		}
		public function loadConfig():void
		{
			var loader:TextLoader = new TextLoader(ConfigObj.absoluteURL+ConfigObj.defaultConfigURL+"type"+type+"/dir"+dir+"/part"+this.part+".xml",this.loadCompleteHandle,null,ioErrorHandle)
			loader.load();
		}
		public function loadCompleteHandle(data:String):void
		{
			var xml:XML = new XML(data);
			if(xml == null || xml.motionXML == null)
			{
				ioErrorHandle();
				return;
			}
			this.index = xml.index;
			this.initX = xml.initX;
			this.initY = xml.initY;
			this.motionXML = xml.motionXML.toString();
			this.motionXML = this.motionXML.substring(11,this.motionXML.length - 12)
			
			
			var tempMotion:Motion = Motion.fromXMLString(this.motionXML);
			if(myDisplayObj == null)
			{
				var tempClass:Class = null;
				
				var tempXML:XML = new XML(this.motionXML)
				
				var empty1:Namespace = tempXML.namespace("");
				var tempXMLL:XMLList = tempXML.empty1::source;
				
				var empty2:Namespace = tempXMLL.namespace("");
				var tempXMLL2:XMLList = tempXMLL.empty2::Source;
				
				var className:String = tempXMLL2.attribute("class");
				
				if(this.mySwfURL != null && AppearanceLoadManager.getInstance().getLoader(this.mySwfURL))
				{
					tempClass = AppearanceLoadManager.getInstance().getClass(this.mySwfURL,className);			
				}
				else
				{
					tempClass = AppearanceLoadManager.getInstance().getClass(this.loaderURL,className);
				}
				if(tempClass == null)
				{
					return;
				}			
				myDisplayObj = new tempClass();
				
				var tempKeyFrame:Keyframe = tempMotion.keyframes[0];
				myDisplayObj.transform.colorTransform = tempKeyFrame.color;
			}
			if(this.color)
			{
				myDisplayObj.transform.colorTransform.alphaOffset = this.color.alphaOffset;
				myDisplayObj.transform.colorTransform.redOffset = this.color.redOffset;
				myDisplayObj.transform.colorTransform.greenOffset = this.color.greenOffset;
				myDisplayObj.transform.colorTransform.blueOffset = this.color.blueOffset;
			}
			
			if(!myDisplayObj)
			{
				ioErrorHandle();
				return;
			}
			myDisplayObj.x = this.initX;
			myDisplayObj.y = this.initY;
			
			myAnimator = Animator.fromXMLString(this.motionXML,myDisplayObj);
			myAnimator.time = 1;
			this.dispatchEvent(new Event("createComplete"))
		}
		public function ioErrorHandle(data:String = null):void
		{
			this.dispatchEvent(new Event("badEquip"))
		}
		public function getXML():XML
		{
			var temp:XML = <equip>
								<part>{this.part}</part>
								<initX>{this.initX}</initX>
								<initY>{this.initY}</initY>
								<motionXML></motionXML>
						   </equip>
			temp.motionXML.appendChild(new XML(this.motionXML));
			return temp;
		}
		public function clear():void
		{
			if(this.myAnimator)
			{
				this.myAnimator.stop();
				this.myAnimator = null;
			}
			if(this.myDisplayObj)
			{
				GCHleper.clearAll(this.myDisplayObj as DisplayObjectContainer);
				this.myDisplayObj = null;
			}
		}
	}
}